new Vue({
    el: "#mainContent",
    data: {
        name: '',
        isLogin: true,
        btnText: "登录",
        userName: "",
        userPwd: "",
        loginUrl: "/login/login.ajax",
        registerUrl: "/login/register.ajax",
        indexUrl: "/index/index"
    },
    mounted: function () {
        console.log("login");
        this.isLogin = ( $("#type").val() == "login");
        this.name = $("#nameUser").val();
        console.log(this.name);
        this.initPage();
    },
    methods: {
        //初始化页面
        initPage: function () {
            console.log(this.isLogin);
            if (this.isLogin) {
                this.btnText = "登录";
            } else {
                this.btnText = "注册";
            }
        },
        submitUser: function () {
            console.log("submit");
            if (this.isLogin) {
                this.$http.post(this.loginUrl, {
                    name: this.userName,
                    pwd: this.userPwd
                }, {emulateJSON: true})
                    .then((response) => {
                        console.log(JSON.stringify(response.data));
                        if (response.data.code == 0) {
                            alert("登录成功");
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.location.href = this.indexUrl;
                        } else {
                            alert("用户名或者密码错误")
                        }
                    })
                    .catch(function (response) {
                        console.log(response)
                    });
            } else {
                this.$http.post(this.registerUrl, {
                    userName: this.userName,
                    pwd: this.userPwd
                }, {emulateJSON: true})
                    .then((response) => {
                        console.log(JSON.stringify(response.data));
                        if (response.data.code == 0) {
                            alert("注册成功");
                        } else {
                            alert("注册失败");
                        }
                    })
                    .catch(function (response) {
                        console.log(response)
                    });
            }
        },
        chooseLogin: function () {
            this.isLogin = true;
            this.btnText = "登录";
        },
        chooseRegister: function () {
            this.isLogin = false;
            this.btnText = "注册";
        }
    }
});